﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Diagnostics;

namespace Cosoware.Web.ResponseFilters
{
    /// <summary>
    /// Counts number of writes to the response stream.
    /// </summary>
    class TextFilterModule : IHttpModule
    {
        private readonly Dictionary<string, string> _contentValues = new Dictionary<string, string>();

        private readonly Dictionary<string, string> _redirectValues = new Dictionary<string, string>();

        /// <summary>
        /// The current application
        /// </summary>
        private HttpApplication _context;

        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
            _context = context;
            _context.BeginRequest += new EventHandler(RegisterContentFilter);
            _context.PreSendRequestHeaders += new EventHandler(RegisterHeaderFilter);
            _contentValues.Add("/kubert_info", String.Empty);
            _redirectValues.Add(":80/kubert_info/", "/");
        }

        /// <summary>
        /// Registers the header filter.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RegisterHeaderFilter(object sender, EventArgs e)
        {
            if (_context.Response.StatusCode == 301 || _context.Response.StatusCode == 302 || _context.Response.StatusCode == 307)
            {
                foreach (var item in _redirectValues)
                {
                    _context.Response.RedirectLocation = _context.Context.Response.RedirectLocation.Replace(item.Key, item.Value);
                }
            }
        }

        /// <summary>
        /// Registers the content filter.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RegisterContentFilter(object sender, EventArgs e)
        {
            foreach (var item in _contentValues)
            {
                _context.Response.Filter = new TextFilterStream(_context.Response.Filter) 
                    { Encoding = _context.Response.ContentEncoding, OldValue = item.Key, NewValue = item.Value };
            }
            
        }

        /// <summary>
        /// Disposes of the resources.
        /// </summary>
        public void Dispose()
        {
        }
    }
}
